package com.yimi.flutter.plugin.flutter_plugin_baidu.methodcall;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import com.yimi.flutter.plugin.flutter_plugin_baidu.asr_plugin.AsrManager;
import com.yimi.flutter.plugin.flutter_plugin_baidu.asr_plugin.OnAsrListener;
import com.yimi.flutter.plugin.flutter_plugin_baidu.asr_plugin.RecogResult;
import com.yimi.flutter.plugin.flutter_plugin_baidu.asr_plugin.ResultStateful;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.Map;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;
import com.yimi.flutter.plugin.flutter_plugin_baidu.app.YmApp;
public class AsrUtil {
    private final static String TAG = "AsrPlugin";
    private static ResultStateful resultStateful;
    private static AsrManager asrManager;

    public static void start(MethodCall call, MethodChannel.Result result) {
        resultStateful = ResultStateful.of(result);

        if (YmApp.context == null) {
            Log.e(TAG, "Ignored start, current activity is null.");
            result.error("Ignored start, current activity is null.", null, null);
            return;
        }
        if (getAsrManager() != null) {
            getAsrManager().start(call.arguments instanceof Map ? (Map) call.arguments : null);
        } else {
            result.error("Ignored start, current getAsrManager is null.", null, null);
        }
    }

    public static void stop() {
        if (asrManager != null) {
            asrManager.stop();
        }
    }

    public static void cancel() {
        if (asrManager != null) {
            asrManager.cancel();
        }
    }

    @Nullable
    private static  AsrManager getAsrManager() {
        if (asrManager == null) {
            asrManager = new AsrManager(YmApp.context, onAsrListener);
        }
        return asrManager;
    }

    private static OnAsrListener onAsrListener = new OnAsrListener() {
        @Override
        public void onAsrReady() {

        }

        @Override
        public void onAsrBegin() {

        }

        @Override
        public void onAsrEnd() {

        }

        @Override
        public void onAsrPartialResult(String[] results, RecogResult recogResult) {

        }

        @Override
        public void onAsrOnlineNluResult(String nluResult) {

        }

        @Override
        public void onAsrFinalResult(String[] results, RecogResult recogResult) {
            if (resultStateful != null) {
                resultStateful.success(results[0]);
            }
        }

        @Override
        public void onAsrFinish(RecogResult recogResult) {

        }

        @Override
        public void onAsrFinishError(int errorCode, int subErrorCode, String descMessage, RecogResult recogResult) {
            if (resultStateful != null) {
                resultStateful.error(descMessage, null, null);
            }
        }

        @Override
        public void onAsrLongFinish() {

        }

        @Override
        public void onAsrVolume(int volumePercent, int volume) {

        }

        @Override
        public void onAsrAudio(byte[] data, int offset, int length) {

        }

        @Override
        public void onAsrExit() {

        }

        @Override
        public void onOfflineLoaded() {

        }

        @Override
        public void onOfflineUnLoaded() {

        }
    };
}
